import React from "react";
import Enterprise from  "./footer.scss";
import { addQueueInfoCreater, addTrucklist, addPoundInfoCreater, addElectInfoCreater, addBurntInfoCreater } from '../../redux/action'
import store from "../../redux/store";
import cookie from 'react-cookies';
import { message } from 'antd';

export class Footer extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
            count: '',
            countArr: [],
            wsuri:'',
            interval:'',
            pingData:''
        }
        store.subscribe((e) => { //监听redux处理后的数据
            this.setState({
                count: store.getState().addname,
                countArr: store.getState().addnamelist
            })
            // console.log(store.getState().addname);//接收redux参数
            // console.log(store.getState().addnamelist);//接收redux数组参数
            
        })
    }
    componentWillUnmount() {       
        window.removeEventListener('resize', this.onWindowResize)
    }
    componentDidMount(){
        let height_bar = document.documentElement.clientHeight;
        let height_main = document.documentElement.clientHeight - 91;//页面内包含面包屑容器。
        let height_list = document.documentElement.clientHeight - 122;//页面内不包含面包屑容器。
        let height_regist = document.documentElement.clientHeight -241;//注册页面的容器。
        let height_instructions = document.documentElement.clientHeight - 116;//使用说明页面的容器。


        // console.log(height_bar)
        let conent =  document.getElementById("conent")
        let nav_bar = document.getElementById("nav_bar")
        let content_main = document.getElementById("ContentMain")
        let nav_bar_main = document.getElementById("navMain")

        let nav_bar_child_list = document.getElementById("nav_bar_child_list")
        let regist = document.getElementById('regist')
        let instructions = document.getElementById('instructions')
        

        // console.log(nav_bar_main)
        if (conent){
            conent.style.minHeight = height_bar + 'px';
        }
        if (nav_bar){
            nav_bar.style.minHeight = height_bar + 'px';
            nav_bar_main.style.height = height_bar - 20 + 'px';
        }
        if (content_main){
            content_main.style.minHeight = height_bar  + 'px';
        }

        if (nav_bar_child_list){
            nav_bar_child_list.style.minHeight = height_bar + 'px';
            console.log('------')
        }


        if (regist){
            regist.style.minHeight = height_regist + 'px';
        }
        if (instructions) {
            instructions.style.minHeight = height_instructions + 'px';
        }
        //针对加载顺序问题，对于子组件获取的延时加载。
        setTimeout(()=>{
            let content_main_list = document.getElementById("ContentMainList")
            
            if (content_main_list){
                content_main_list.style.minHeight = height_bar + 'px';
            }

            let nav_bar_child_list = document.getElementById("nav_bar_child_list")
            let nav_bar_child_list_nav = document.getElementById("nav_bar_child_list_nav")
            if (nav_bar_child_list) {
                nav_bar_child_list.style.minHeight = height_bar + 'px';
                nav_bar_child_list_nav.style.height = height_bar/2 + 180 + 'px';
            }

        },500)
        
        window.addEventListener('resize', this.onWindowResize)
        //ws地址
        // if (sessionStorage.getItem('urlConfig')) {
        //     const wsuri = JSON.parse(sessionStorage.getItem('urlConfig')).websocket_url;
        //     // const wsuri = 'ws://sxhlny.api.y8zh.com:8381';
        //     const interval = JSON.parse(sessionStorage.getItem('urlConfig')).websocket_ping_interval * 1000;
        //     const pingData = JSON.parse(sessionStorage.getItem('urlConfig')).websocket_ping_data;
        //     this.setState({
        //         wsuri, interval, pingData
        //     },()=>{
        //         this.initWebSocket();
        //     })
            
        // }
    }
    initWebSocket = () => {
        const { wsuri } = this.state;
        this.websock = new WebSocket(wsuri);
        this.websock.onopen = this.websocketonopen;
        this.websock.onerror = this.websocketonerror;
        this.websock.onmessage = this.websocketonmessage;
        this.websock.onclose = this.websocketclose;
    }
    websocketonopen = (e) => {
        // console.log("WebSocket连接成功");
    }
    websocketonerror = (e) => {
        // console.log("WebSocket连接发生错误");
    }
    websocketonmessage = (e) => { //数据接收
        let _this = this;
        const { pingData, interval } = this.state;
        const redata = JSON.parse(e.data).data;
        if (!redata) {
            return;
        }
        // console.log(redata)
        if (!cookie.load('station_console_setting_web_login') || !cookie.load('station_console_setting_web_login').auth_token) {
            message.error('登录失效请重新登录！', 1, () => {
                sessionStorage.removeItem('urlConfig');
                window.location.href = '/login';
            })
            return;
        }
        if (redata.type == 'connect') {
            let msg_content = new Object();
            msg_content['app_type'] = '29'
            msg_content['auth_token'] = cookie.load('station_console_setting_web_login').auth_token
            let data = new Object();
            data['type'] = "bind";
            data['data'] = msg_content;
            // console.log(data, 'WebSocket连接成功---')
            this.bindData = JSON.stringify(data);
            this.websocketsend(this.bindData)
        }
        if (redata.type == 'bind') {
            // console.log(pingData, 'WebSocket绑定成功---')
            this.bindData = JSON.stringify(pingData);
            this.setint = setInterval(function () {
                _this.websocketsend(_this.bindData)
                // console.log('ping')
            }, interval)
        }
        //云排号监察推送
        if (redata.type == 'queue_live') { //云排号监察推送---车辆排号实况
            if (window.location.pathname === '/index/cloudArrangingmonitor/carArrangingActually') {
                // console.log('推送---queue_live')
                const queue_info = redata.queue_info;
                store.dispatch(addQueueInfoCreater(queue_info));
            }
        }


        if (redata.type == 'jyjd_visual-time_out') {//停止所有操作
            if (sessionStorage.getItem('token')) {
                // 退出登录
                // console.log('长时间未操作，退出登录。')
                this.$store.commit('setLoginState', 0);//在vuex中设置当前状态为退出状态
                this.$store.commit('setTimeOut', 1);//在vuex中设置当前状态为已超时状态
                sessionStorage.clear();
                this.$alert('<div>由于长时间未操作，数据屏已进入数据 <span>安全保护状态</span></div><div class="cent">如需继续查看，请点击按钮重新连接 </div>', '', {
                    dangerouslyUseHTMLString: true,
                    customClass: 'timoOut',
                    showClose: false,
                    confirmButtonText: '重新连接'
                }).then(() => {
                    this.$router.push('/');
                });
            }

        }
    }
    websocketsend = (agentData) => { //数据发送
        this.websock.send(agentData);
    }
    websocketclose = (e) => { //关闭
        let _this = this;
        const { interval } = this.state;
        // console.log("connection closed (" + e.code + ")");
        clearInterval(this.setint);
        setTimeout(function () {
            _this.initWebSocket()
        }, 10000)
    }
    componentWillReceiveProps = () => {
        setTimeout(() => {
            
            let height_list = document.documentElement.clientHeight;//页面内不包含面包屑容器。
            let content_main_list = document.getElementById("ContentMainList");

            if (content_main_list){
                content_main_list.style.minHeight = height_list + 'px';
            }

            let nav_bar_child_list = document.getElementById("nav_bar_child_list")
            let nav_bar_child_list_nav = document.getElementById("nav_bar_child_list_nav")
            if (nav_bar_child_list) {
                nav_bar_child_list.style.minHeight = height_list + 'px';
                nav_bar_child_list_nav.style.height = height_list / 2 + 180 + 'px';
            }
        }, 500)
    }
    onWindowResize = () => {
     
        let height_bar = document.documentElement.clientHeight;
        let height_main = document.documentElement.clientHeight - 161;
        let height_regist = document.documentElement.clientHeight - 311;
        
        // console.log(height_main,'--------------------')
        let conent =  document.getElementById("conent")
        let nav_bar = document.getElementById("nav_bar")
        let content_main = document.getElementById("ContentMain")
        let nav_bar_main = document.getElementById("navMain")
        

        let regist = document.getElementById('regist')
        // console.log(conent)
        if (conent) {
            conent.style.minHeight = height_bar + 'px';
        }
        if (nav_bar) {
            nav_bar.style.minHeight = height_bar + 'px';
            nav_bar_main.style.height = height_bar - 20 + 'px';
        }
        if (content_main) {
            content_main.style.minHeight = height_bar + 'px';
        }


        if (regist) {
            regist.style.minHeight = height_regist + 'px';
        }
        setTimeout(() => {
            
            let height_list = document.documentElement.clientHeight - 193;//页面内不包含面包屑容器。
            let content_main_list = document.getElementById("ContentMainList");
            
            let nav_bar_child_list = document.getElementById("nav_bar_child_list")
            let nav_bar_child_list_nav = document.getElementById("nav_bar_child_list_nav")
            if (nav_bar_child_list) {
                nav_bar_child_list.style.minHeight = height_bar + 'px';
                nav_bar_child_list_nav.style.height = height_bar / 2 + 180 + 'px';
            }
            if (content_main_list) {
                content_main_list.style.minHeight = height_bar + 'px';
            }
        }, 500)
    }
    render(){
        return(
            <div className={Enterprise.footer}>
                {/* Copyright 2019 上海预石网络科技有限公司 All Rights Reserved www.yun8zhaohuo.com   总部地址：上海市徐汇区龙华中路600号绿地中心B座1707 沪B2-20170026 */}
            </div>
        )
    }
}

export default Footer;